import { list, find as productFind } from "../../api/product";
import { list as warehouseList } from "../../api/warehouse";
import { find as customerFind, list as customerList } from "../../api/customer";
import { datetime } from "../../utils/util";
Page({

    data: {
        flag: false,
        triangle_Activ: '/static/triangle-activ.png',
        triangle: '/static/triangle.png',
        filterActiv: null,
        state: ['全部', '审核中', '已上架', '已下架', '已删除'],
        stateName: '',
        stateActiv: null,
        company: [],
        companyActiv: null,
        companyName: '',
        storehouse: [],
        storeName: '',
        storeActiv: null,
        // storeIndex: 0,
        // storeLev2: [],
        selectName: '',
        query: {
            keyWord: '',
            pageNum: 1,
            pageSize: 20,
        },
        productContent: [],
        memberName: [],
        date: [],
        searchSupply: '',
        searchOilStore: '',
        productId: '',
        timer: null,
    },

    onShow() {
        this.data.query.pageNum = 1
        this.data.query.pageSize = 20
        this.setData({
            productContent: [],
            [`query.pageNum`]: this.data.query.pageNum,
            [`query.pageSize`]: this.data.query.pageSize,
        })

        this.getProduct(this.data.query.keyWord);
        this.getSupplyList();
        this.getCustomerList();
    },
    onPullDownRefresh() {
        wx.showLoading({
            title: '加载中',
        })
        this.data.productContent = []
        this.data.query.pageNum = 1
        this.setData({
            [`query.pageNum`]: this.data.query.pageNum,
            productContent: this.data.productContent
        })
        setTimeout(() => {
            this.getProduct('','',() => {
                wx.hideLoading()
                wx.stopPullDownRefresh()
            })
        }, 1000)
    },
    onReachBottom() {
        this.data.query.pageNum += 1
        this.setData({
            [`query.pageNum`]: this.data.query.pageNum
        })
        this.getProduct()
    },

    //截取字符串字数大于4，取前面4位加省略号
    sliceString(item) {
        return item.length > 4 ? item.slice(0, 4) + '...' : item;
    },

    popup(e) {
        let flag = !this.data.flag;
        let _this = this;
        let data = this.data;
        this.setData({
            flag: flag,
            filterActiv: e.currentTarget.dataset.popup
        })
        // 初始化油库二级列表
        // if (data.filterActiv == 2) {
        //     _this.setData({
        //         storeLev2: data.testStorehouse[data.storeIndex].info,
        //     })
        // }
    },

    tapState(e) {
        let _this = this;
        let data = this.data;
        let itemName = e.currentTarget.dataset.name;
        let state;
        if (itemName == '全部') {
            state = '';
        } else {
            state = _this.sliceString(itemName);
        }
        _this.setData({
            stateActiv: e.currentTarget.dataset.state,
            stateName: state,
            flag: false
        })
    },
    //供应商
    tapCompany(e) {
        let _this = this;
        let itemName = e.currentTarget.dataset.name;
        let company = '';
        this.data.productId = e.currentTarget.dataset.id;
        this.getProduct('', e.currentTarget.dataset.id)
        if (itemName == '全部') {
            company = '';
        } else {
            company = _this.sliceString(itemName);
        }
        _this.setData({
            companyActiv: e.currentTarget.dataset.company,
            companyName: company,
            flag: false
        })
    },

    //油库
    // tapStorehouse(e) {
    //     let storehouse_index = e.currentTarget.dataset.storehouse_index
    //     let index = storehouse_index != '' ? storehouse_index : 0;
    //     let _this = this;
    //     let data = _this.data;
    //     _this.setData({
    //         storeLev2: data.testStorehouse[index].info,
    //         storeIndex: index,
    //     })
    // },
    tapStorehouseItem(e) {
        console.log(e.currentTarget.dataset.store_item, e.currentTarget.dataset.store_item_name);
        let _this = this;
        let itemName = e.currentTarget.dataset.store_item_name;
        let storeItem = '';

        if (itemName == '全部') {
            storeItem = '';
        } else {
            storeItem = _this.sliceString(itemName);
        }
        _this.setData({
            storeActiv: e.currentTarget.dataset.store_item,
            storeName: storeItem,
            selectName: itemName,
            flag: false
        })
    },

    //输入框输入
    search(e) {
        console.log(e.detail.value);
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(() => {
            this.data.query.keyWord = e.detail.value;
            let branch = e.currentTarget.dataset.porduct;
            if (branch == 0) {
                this.getProduct(this.data.query.keyWord)
            } else if (branch == 1) {
                this.getCustomerList(this.data.searchSupply)
            }
        }, 500)
    },
    searchSupply(e) {
        console.log(e.detail.value);
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(() => {
            this.data.searchSupply = e.detail.value;
            let branch = e.currentTarget.dataset.porduct;
            if (branch == 0) {
                this.getProduct(this.data.query.keyWord)
            } else if (branch == 1) {
                this.getCustomerList(this.data.searchSupply)
            }
        }, 500)
    },
    //输入框回车
    bindconfirm(e) {
        console.log(e);
        let branch = e.currentTarget.dataset.porduct;
        if (branch == 0) {
            this.getProduct(this.data.query.keyWord)
        } else if (branch == 1) {
            this.getCustomerList(this.data.searchSupply)
        }
    },

    //新增按钮
    add() {
        wx.navigateTo({
            url: '/pages/product/addProduct',
        })
    },
    getProduct(keyword, customerId, callBack) {
        let _this = this;
        let memberName = [];
        let date = [];
        let params = { 
            keyword: keyword, 
            customerId: customerId, 
            direction: 'asc',
            pageNum: this.data.query.pageNum,
            pageSize: this.data.query.pageSize,
        }
        list(params).then(res => {
            console.log(res);
            let product = res.data.data
            for (let i = 0; i < product.length; i++) {
                let findMemberId = product[i].customerId;
                let productId = product[i].id;
                customerFind(findMemberId).then(res => {
                    console.log(res, '2');
                    if (res.data.customer != null) {
                        memberName.push({ id: findMemberId, name: res.data.customer.name, productId: productId });
                    } else {
                        memberName.push({ name: '该卖家异常', productId: productId });
                    }
                    _this.setData({
                        memberName: memberName
                    });
                });
                productFind(productId).then(res => {
                    let dates = datetime(res.data.customer.createDate);
                    date.push({ id: productId, date: dates });
                    _this.setData({
                        date: date
                    });
                });
            }
            this.setData({
                productContent: res.data.data,
            })
        })
        //如果回调函数存在，则执行该回调函数。
        callBack && callBack()
    },
    goToDetail(e) {
        wx.navigateTo({
            url: '/pages/product/productDetail?id=' + e.currentTarget.dataset.id,
        })
    },
    getCustomerList(keyword) {
        customerList({
            keyword: keyword,
            type: 1
        })
            .then(res => {
                this.setData({
                    company: res.data.data
                })
            })
    },
    getSupplyList() {
        warehouseList().then(res => {
            this.setData({
                storehouse: res.data.data
            })
        })
    },
})
